﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using InfoPick.Lib.Tools;
using InfoPick.Lib.Entity;
using InfoPick.Lib.StringResolve;

namespace InfoPick.Lib.DataProvider
{
    public class TextProvider : DataProvider
    {
        //mgw文件地址
        private string mgwFile;

        //msc文件地址
        private string mscFile;

        //mgw文件内容
        private string mgwContents;

        //msc文件内容
        private string mscContents;

        public TextProvider(string mgwFile, string mscFile)
        {
            this.mgwFile = mgwFile;
            this.mscFile = mscFile;
        }


        public override bool Begin()
        {
            Notice("开始分析文本！\r");
            mgwContents = File.ReadAllText(mgwFile, TextEnCoding);
            mgwContents = mgwContents.Replace("\r\0\r\n", "\r\n");
            mgwContents = mgwContents.Replace("\r\n\n", "\r\n");

            mscContents = File.ReadAllText(mscFile, TextEnCoding);
            mscContents = mscContents.Replace("\r\0\r\n", "\r\n");
            mscContents = mscContents.Replace("\r\n\n", "\r\n");
            return true;
        }

        public override void Complete()
        {
                
        }

        private string getContents(string key)
        {
            if (dict[key].ToLower() == "mgw")
                return mgwContents;
            if (dict[key].ToLower() == "msc")
                return mscContents;
            throw new Exception(string.Format("{0}的配置只能为‘mgw’或者‘msc’", key));
        }

        protected override void CmdSuccess(string cmd, string res)
        {
            if (OnCmdSuccess != null)
            {
                string type = dict[cmd];
                string key = string.IsNullOrEmpty(res) ? string.Format("{0}：{1}（未找到）", type, cmd) : string.Format("{0}：{1}", type, cmd);

                OnCmdSuccess(key, res);
            }
        }

        public override string GetTdmiuList()
        {
            const string startIdentifier = "LST TDMIU";
            string[] endIdentifier = new string[] { "结果个数", "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("TDMIU"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("TDMIU", res);
            return res;
        }

        public override string GetE32List()
        {
            const string startIdentifier = "LST BRD: LM=BTBN, BT=E32";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("E32"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("E32", res);
            return res;
        }

        public override string GetS2lList()
        {
            const string startIdentifier = "LST BRD: LM=BTBN, BT=S2L";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("S2L"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("S2L", res);
            
            return res;
        }

        public override string GetEpiList()
        {
            const string startIdentifier = "LST BRD: BT=WEPI";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("EPI"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("EPI", res);
            return res;
        }

        public override string GetCsuList()
        {
            const string startIdentifier = "LST BRD: BT=WCSU";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("CSU"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("CSU", res);
            
            return res;
        }

        public override string GetN7LNKALLList()
        {
            const string startIdentifier = "LST N7LNK: LTP=LOCAL";
            string[] endIdentifier = new string[] { "结果个数", "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("N7LNKALL"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("N7LNKALL", res);
            
            return res;
        }

        public override string GetAIETKCList()
        {
            const string startIdentifier = "LST AIETKC: MODE=BYCIC";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("AIETKC"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("AIETKC", res);
            
            return res;
        }

        public override string GetN7TKCList()
        {
            const string startIdentifier = "LST N7TKC: QR=LOCAL";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("N7TKC"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("N7TKC", res);
            return res;
        }



        public override string GetAIETGList()
        {
            string startIdentifier = "LST AIETG:";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("AIETG"), startIdentifier, endIdentifier, 0, true, true);

            CmdSuccess("AIETG", res);
            
            return res;
        }

        public override string GetN7LNKList()
        {

            string startIdentifier = "LST N7LNK:";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("N7LNK"), startIdentifier, endIdentifier, 0, true, true);
            CmdSuccess("N7LNK", res);
            
            return res;
        }


        public override string GetMTP2LNKAllList()
        {

            string startIdentifier = "LST MTP2LNK:";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("MTP2LNK"), startIdentifier, endIdentifier, 0, true, true);
            CmdSuccess("MTP2LNK", res);
            
            return res;
        }

        public override string GetMTP2LNKList(int key)
        {

            string startIdentifier = string.Format("LST MTP2LNK: LNKNO={0}", key);
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("MTP2LNK"), startIdentifier, endIdentifier, 0, true, true);
           
            string type = dict["MTP2LNK"];
            string cmd = string.IsNullOrEmpty(res) ? string.Format(type + "：MTP2LNK:{0}（未找到），将在MTP2LNK list中查询", key) : string.Format(type + "：MTP2LNK:{0}", key);

            base.CmdSuccess(cmd, res);
            return res;
        }

        public override string GetCLKSRCList()
        {

            const string startIdentifier = "LST CLKSRC";
            string[] endIdentifier = new string[] { "结果个数", "---    END" };
            string res = StringTools.AdvSubString(getContents("CLKSRC"), startIdentifier, endIdentifier, 0, true, true);
            CmdSuccess("CLKSRC", res);
           
            return res;
        }


        private static readonly string textEncoding = ConfigurationManager.AppSettings["TextEnCoding"];
        private static Encoding TextEnCoding
        {
            get
            {
                var code = Encoding.GetEncoding(textEncoding);
                if (code != null)
                    return code;
                return Encoding.UTF8;
            }
        }
    }
}
